package com.yqg.protection.netty.service;

import com.yqg.protection.service.IFireSysStateDataService;
import com.yqg.protection.service.ITemporaryDataService;
import com.yqg.protection.utils.ByteUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * projectName:xf-service
 * author:zxf
 * time:2025/8/1 17:44
 * description:
 */
@Service
@Slf4j
public class KeAn5d6cService extends ProtocolService {

    @Autowired
    IFireSysStateDataService fireSysStateDataService;
    @Autowired
    ITemporaryDataService temporaryDataService;

    public void analyzeTemperData(String hexToNum, String saveTime, String raw, String cmd, String src) {
        log.info("设备识别码：" + src);
        log.info("命令字：" + cmd);
        log.info("数据单元：" + raw);
        log.info("保存时间：" + saveTime);
        log.info("流水号：" + hexToNum);
        //设备注册
        if (cmd.equals("1")) {
            String ICCID = raw.substring(0, 40);
            log.info("ICCID: "+ICCID);
            String IMSI = raw.substring(40, 70);
            log.info("IMSI: "+IMSI);
            String hardwareVersionStr1 = raw.substring(70, 71);
            Integer hardwareVersion1 = Integer.parseInt(hardwareVersionStr1, 16);
            String hardwareVersionStr2 = raw.substring(71, 72);
            Integer hardwareVersion2 = Integer.parseInt(hardwareVersionStr2, 16);
            String hardwareVersion = "V" + hardwareVersion1 + "." + hardwareVersion2;
            log.info("设备硬件版本: "+hardwareVersion);
            String softwareVersionStr1 = raw.substring(72, 73);
            Integer softwareVersion1 = Integer.parseInt(softwareVersionStr1, 16);
            String softwareVersionStr2 = raw.substring(73, 74);
            Integer softwareVersion2 = Integer.parseInt(softwareVersionStr2, 16);
            String softwareVersion = "V" + softwareVersion1 + "." + softwareVersion2;
            log.info("设备软件版本: "+softwareVersion);
            String batteryVoltageStr = raw.substring(74, 76);
            Double batteryVoltage = Integer.parseInt(batteryVoltageStr, 16) * 0.1;
            log.info("电池电压: "+batteryVoltage + "V");
            String batteryLevelStr = raw.substring(76, 78);
            Integer batteryLevel = Integer.parseInt(batteryLevelStr, 16);
            log.info("电池电量: "+batteryLevel + "%");
            String signStrengthStr = raw.substring(78, 80);
            Integer signStrength = Integer.parseInt(signStrengthStr, 16);
            //取值范围：[0,31]  99：无服务
            log.info("信号强度: "+signStrength);
        }
        if (cmd.equals("4")) {
            String ICCID = raw.substring(0, 40);
            log.info("ICCID: "+ICCID);
            String warnStateStr = raw.substring(40, 44);
            List<String> warnStateList = new ArrayList<>();
            byte[] warnStateArray = ByteUtil.getBooleanArray(ByteUtil.hexStringToByteArray(warnStateStr));
            for (int j = 0; j < warnStateArray.length; j++) {
                if (warnStateArray[j] == 1) {
                    String warnState = String.valueOf(15 - j);
                    warnStateList.add(warnState);
                }
            }
            log.info("报警状态: "+warnStateList);
            String warnAction = raw.substring(44, 46);
            log.info("报警动作: "+warnAction);
            String breakStateStr = raw.substring(46, 48);
            List<String> breakStateList = new ArrayList<>();
            byte[] breakStateArray = ByteUtil.getBooleanArray(ByteUtil.hexStringToByteArray(breakStateStr));
            for (int j = 0; j < breakStateArray.length; j++) {
                if (breakStateArray[j] == 1) {
                    String breakState = String.valueOf(7 - j);
                    breakStateList.add(breakState);
                }
            }
            log.info("故障状态: "+breakStateList);
            String univerStateStr = raw.substring(48, 52);
            List<String> univerStateList = new ArrayList<>();
            byte[] univerStateArray = ByteUtil.getBooleanArray(ByteUtil.hexStringToByteArray(univerStateStr));
            for (int j = 0; j < univerStateArray.length; j++) {
                if (univerStateArray[j] == 1) {
                    String univerState = String.valueOf(15 - j);
                    univerStateList.add(univerState);
                }
            }
            log.info("通用状态: "+univerStateList);
            String batteryVoltageStr = raw.substring(52, 54);
            Double batteryVoltage = Integer.parseInt(batteryVoltageStr, 16) * 0.1;
            log.info("电池电压: "+batteryVoltage + "V");
            String batteryLevelStr = raw.substring(54, 56);
            Integer batteryLevel = Integer.parseInt(batteryLevelStr, 16);
            log.info("电池电量: "+batteryLevel + "%");
            String signStrengthStr = raw.substring(56, 58);
            Integer signStrength = Integer.parseInt(signStrengthStr, 16);
            //取值范围：[0,31]  99：无服务
            log.info("信号强度: "+signStrength);
            String operatParam = raw.substring(58);
            log.info("设备运行参数: "+operatParam);
        }
    }
}
